﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tzync
{
    public partial class AddNewProfileUIForm : Form
    {
        private Profile profile;    //profile to be added
        private SyncTool syncTool;  //facade class
        private String rootAddress; //address of selected folder to sync

        public AddNewProfileUIForm(SyncTool newSyncTool)
        {
            syncTool = newSyncTool;
            InitializeComponent();
        }

        private void AddNewProfileUIForm_Load(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            //tooltips
            browseBtnToolTip.SetToolTip(browseBtn, "Click here to browse for a folder to sync");
            applyBtnToolTip.SetToolTip(createProfileBtn, "Click here to create a new profile using the information above");
            cancelBtnToolTip.SetToolTip(cancelBtn, "Click here to cancel and return to the Main screen");
        }

        private void AddNewProfileUIForm_Shown(object sender, EventArgs e)
        {
            profileNameTB.Focus();
            infoLabel.Text = "";
        }

        private void AddNewProfileUIForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            DialogResult result = syncFolderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                rootAddress = syncFolderBrowserDialog.SelectedPath;
                syncFolderTB.Text = rootAddress;
            }
            else
                return;
        }

        //drag folder functions
        private void AddNewProfileUIForm_DragEnter(object sender, DragEventArgs e)
        {
            infoLabel.Text = "";

            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                String[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (Directory.Exists(files[0]))
                    e.Effect = DragDropEffects.All;
            }
        }

        private void AddNewProfileUIForm_DragDrop(object sender, DragEventArgs e)
        {
            String[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            rootAddress = files[0];
            syncFolderTB.Text = rootAddress;
        }

        private void profileNameTB_TextChanged(object sender, EventArgs e)
        {
            infoLabel.Text = "";
        }

        private void syncFolderTB_TextChanged(object sender, EventArgs e)
        {
            infoLabel.Text = "";
        }

        private void createProfileBtn_Click(object sender, EventArgs e)
        {
            if (profileNameTB.Text == "")
            {
                infoLabel.Text = "Please enter a valid profile name!";
                profileNameTB.Focus();
                return;
            }
            else if (!Directory.Exists(syncFolderTB.Text))
            {
                infoLabel.Text = "Please enter a valid folder to sync!";
                syncFolderTB.Focus();
                return;
            }

            try
            {
                //create profile with selected folder
                syncTool.CreateProfile(profileNameTB.Text, syncFolderTB.Text);
            }
            catch (ProfileNotUniqueException)
            {
                infoLabel.Text = "A profile with the same name already exists!";
                profileNameTB.Focus();
                return;
            }

            profile = syncTool.GetProfile();
            
            if (profile.FirstTime == true)  //first sync
            {
                infoLabel.Text = "Syncing in progress...!";
                Cursor.Current = Cursors.WaitCursor;
                syncTool.WriteProfile();
                syncTool.LoadMetadata();    //load metadata
                syncTool.Sync();    //copy files
                Cursor.Current = Cursors.Default;

                DialogResult syncDialog = MessageBox.Show("As this is your first sync, your files has been automatically synced!",
                "Important!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            this.Close();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public String GetName()
        {
            return profileNameTB.Text;
        }

        public String GetRootAddress()
        {
            return syncFolderTB.Text;
        }
    }
}
